﻿using LibraryManagement.Domain;
using System.Data.Entity.ModelConfiguration;

namespace LibraryManagement.DataAccess.Configuration
{
    public class BookConfig: EntityTypeConfiguration<Book>
    {
        public BookConfig()
        {
            this.HasKey(b => b.Id);

            this.HasMany(b => b.Authors)
                .WithMany(a => a.Books)
                .Map(db => db.MapLeftKey("BookId").MapRightKey("AuthorId").ToTable("BookAuthor"));

            this.HasMany(b => b.Categories)
                .WithMany(bc => bc.Books)
                .Map(db => db.MapLeftKey("BookId").MapRightKey("CategoryId").ToTable("BookCategory"));

            this.HasRequired(b => b.Publisher)
                .WithMany(p => p.Books)
                .HasForeignKey(b => b.PublisherId);

            this.HasOptional(b => b.Borrower)
                .WithMany(u => u.BorrowingBooks)
                .HasForeignKey(b => b.BorrowerId);

            this.HasRequired(b => b.Publisher);

            this.Property(b => b.Status).IsRequired();
        }
    }
}
